package com.lc.zpyh.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.lc.zpyh.R;
import com.lc.zpyh.http.response.SelectCanteenBySchoolBean;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public  class SearchPopupWindow extends PopupWindow implements View.OnClickListener {

    private Context context;
    private View mPopView;
    private List<SelectCanteenBySchoolBean.ListBean> dataList=new ArrayList();
    private TextView tv_quanbu;
    private OnItemLickenter onItemLickenter;
    private RecyclerView recyclerView;
    private int type;
    private BaseQuickAdapter<SelectCanteenBySchoolBean.ListBean, BaseViewHolder> adapter;
    public SearchPopupWindow(Context context, List dataList) {
        super(context);
        this.context = context;
        this.dataList=dataList;
        init(context);
        setPopupWindow();

    }

    private void init(Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        mPopView = inflater.inflate(R.layout.popup_search, null);
        tv_quanbu = mPopView.findViewById(R.id.tv_quanbu);
        tv_quanbu.setOnClickListener(this);
        recyclerView = mPopView.findViewById(R.id.recyclerView);
        adapter = new BaseQuickAdapter<SelectCanteenBySchoolBean.ListBean, BaseViewHolder>
                (R.layout.item_popwindow_search,dataList) {

            @Override
            protected void convert(BaseViewHolder baseViewHolder,SelectCanteenBySchoolBean.ListBean dataBean) {
                baseViewHolder.setText(R.id.tv_name,dataBean.getName());
            }


        };

        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                if(onItemLickenter!=null){
                    onItemLickenter.onTypeClick(dataList.get(position).getId());

                }
                dismiss();
            }
        });

        recyclerView.setAdapter(adapter);
    }
    @SuppressLint("InlinedApi")
    private void setPopupWindow() {
        this.setContentView(mPopView);// 设置View
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);// 设置弹出窗口的宽
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);// 设置弹出窗口的高
        this.setFocusable(true);// 设置弹出窗口可
//        this.setAnimationStyle(R.style.DetailAnimation);// 设置动画
        this.setBackgroundDrawable(new ColorDrawable(0x00000000));// 设置背景透明
        this.setOutsideTouchable(true);
//        this.setOnDismissListener(new OnDismissListener() {
//
//            @Override
//            public void onDismiss() {
//                WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();
//                lp.alpha = 1f;
//                ((Activity) context). getWindow().setAttributes(lp);
//            }
//        });

    }

    @Override
    public void onClick(View view) {
        if(onItemLickenter!=null){
            onItemLickenter.onTypeClick(type);
        }
        dismiss();
    }
    public void setOnItemLickenter(OnItemLickenter onItemLickenter){
        this.onItemLickenter=onItemLickenter;
    }
    public interface OnItemLickenter{
        void onTypeClick(int type);

    }
}
